NLLLoss

对输入的对数概率(log-probabilities)和目标标签计算负对数似然损失(Negative Log Likelihood Loss)。

该算子通常用于分类任务中,输入为已经取对数的概率值(例如 LogSoftmax 的输出)。

\[\text{loss}_i = - \log p_{i, y_i} \cdot w_{y_i}\]

其中:

  • \(p_{i, y_i}\) 表示第 \(i\) 个样本在真实类别 \(y_i\) 上的预测概率

  • \(w_{y_i}\) 表示对应类别的权重

根据 reduction_type 的不同,对 batch 维度的损失进行不同方式的归约。

输入:
  • log_probs - 输入的对数概率数据地址,形状为 [batch_size, class_num]

  • labels - 真实标签索引地址,形状为 [batch_size]

  • weight - 类别权重数组地址,形状为 [class_num]

  • batch_size - batch 大小。

  • class_num - 类别数量。

  • reduction_type - 损失归约方式:
    • 0:None,不做归约,逐样本输出

    • 1:Sum,对 batch 内损失求和

    • 2:Mean,对 batch 内损失按权重和求平均

  • core_mask - 核掩码(仅适用于共享存储版本)。

输出:
  • loss - 损失输出地址:
    • reduction_type = 0 时,输出长度为 batch_size

    • reduction_type = 12 时,仅使用 loss[0]

  • total_weight - 所有样本权重之和地址。

支持平台:

FT78NE MT7004

备注

  • FT78NE 支持 fp, int8

  • MT7004 支持 hp, fp

  • 输入 log_probs 应已是对数概率值

  • labels 中的索引需满足 0 <= label < class_num

共享存储版本:

void i8_nllloss_s(const int8_t *log_probs, const int *labels, const int8_t *weight, int32_t *loss, int32_t *total_weight, int batch_size, int class_num, int reduction_type, int core_mask)
void hp_nllloss_s(const half *log_probs, const int *labels, const half *weight, half *loss, half *total_weight, int batch_size, int class_num, int reduction_type, int core_mask)
void fp_nllloss_s(const float *log_probs, const int *labels, const float *weight, float *loss, float *total_weight, int batch_size, int class_num, int reduction_type, int core_mask)

C调用示例:

 1//FT78NE示例
 2#include <stdio.h>
 3#include <nllloss.h>
 4
 5int main(int argc, char* argv[]) {
 6    float *log_probs = (float *)0xA0000000;   // [batch_size, class_num]
 7    int   *labels    = (int *)0xA0001000;     // [batch_size]
 8    float *weight    = (float *)0xA0002000;   // [class_num]
 9    float *loss      = (float *)0xC0000000;
10    float *total_w   = (float *)0xC0001000;
11    int batch_size = 32;
12    int class_num  = 1000;
13    int reduction_type = 2;   // Mean
14    int core_mask = 0xff;
15
16    fp_nllloss_s(log_probs, labels, weight, loss, total_w,
17                 batch_size, class_num, reduction_type, core_mask);
18    return 0;
19}

私有存储版本:

void i8_nllloss_p(const int8_t *log_probs, const int *labels, const int8_t *weight, int32_t *loss, int32_t *total_weight, int batch_size, int class_num, int reduction_type)
void hp_nllloss_p(const half *log_probs, const int *labels, const half *weight, half *loss, half *total_weight, int batch_size, int class_num, int reduction_type)
void fp_nllloss_p(const float *log_probs, const int *labels, const float *weight, float *loss, float *total_weight, int batch_size, int class_num, int reduction_type)

C调用示例:

 1//FT78NE示例
 2#include <stdio.h>
 3#include <nllloss.h>
 4
 5int main(int argc, char* argv[]) {
 6    float *log_probs = (float *)0x10810000;   // L2空间
 7    int   *labels    = (int *)0x10820000;
 8    float *weight    = (float *)0x10830000;
 9    float *loss      = (float *)0x10840000;
10    float *total_w   = (float *)0x10850000;
11    int batch_size = 32;
12    int class_num  = 1000;
13    int reduction_type = 1;   // Sum
14
15    fp_nllloss_p(log_probs, labels, weight, loss, total_w,
16                 batch_size, class_num, reduction_type);
17    return 0;
18}